﻿<?php

/**

 * 我的代金券

 *

 *

 *

 *

 * @copyright  Copyright (c) 2007-2015 ShopNC Inc. (http://www.shopnc.net)

 * @license    http://www.shopnc.net

 * @link       http://www.shopnc.net

 * @since      File available since Release v1.1

 */

defined('ByShopWWI') or exit('Access Invalid!');

class member_pointsControl extends mobileMemberControl {

	public function __construct() {

		parent::__construct();

	}

	/**

	 * 签到列表

	 */

	public function pointslogOp() {

		$condition_arr = $list_log = array();

		$condition_arr['pl_memberid'] = $this -> member_info['member_id'];
		$condition_arr['pl_stage'] = 'turnpoints';
		//分页

		$page = new Page();
		$page->setEachNum(intval($_REQUEST['page']));
		$points_model = Model('points');

		$list_log = $points_model -> getPointsLogList($condition_arr, $page, '*', '');

		if (!empty($list_log)) {

			foreach ($list_log as $key => $value) {

				$list_log[$key]['stagetext'] = $this -> insertarr($value['pl_stage']);

				$list_log[$key]['addtimetext'] = date('Y-m-d H:i:s', $value['pl_addtime']);

			}

		}

		$page_count = $page -> gettotalpage();
		output_data(array('log_list' => $list_log), mobile_page($page_count));

	}

	public function memberpointsOp() {

		$model_member = Model('member');

		$red_points = $_REQUEST['red_points'];

		//查询红积分数量

		//$red_points_y  shopwwi_returnpoint

		//$sql="select * from ";

		$obj_member2 = Model('member');

		$member_jf = $obj_member2 -> where(array('member_id' => $this -> member_info['member_id'])) -> select();

		//exit(print_r($member_jf));

		//exit(print_r($member_jf[0]['member_id']));

		//exit(print_r($member_tj));

		//

		$returnpoints = Model('returnpoint');

		$order = ' order by addtime desc';

		$repoint = $returnpoints -> order($order) -> select();

		$repoints = $repoint['back_point'];

		$now_red_points = $member_jf[0]['member_red_points'];

		//exit(print_r($now_red_points));

		$pl_addtime = strtotime(date('Y-m-d H:i:s'));

		if ($now_red_points >= 100) {
			//红积分提现条件:分红豆大于000且提现比例为100的倍数

			//print_r();

			if ($red_points % 100 == 0) {

				$red_pointcash = ($red_points * $repoints) / 100;
				//红积分提现比例

				$member_array = array();

				// Model()->execute('update shopwwi_member set available_predeposit = available_predeposit + '.$red_pointcash.' where member_id='.$_SESSION['member_id']);

				$member_array['available_predeposit'] = $member_jf[0]['available_predeposit'] + $red_points/100;//100表示分红豆兑换余额比例为100比1

				$update = $model_member -> editMember(array('member_id' => $this -> member_info['member_id']), $member_array);

				//减去分红豆

				Model() -> execute('update shopwwi_member set member_red_points = member_red_points - ' . $red_points . ' where member_id=' . $this -> member_info['member_id']);

				$memberid = $member_jf[0]['member_id'];

				$membername = $member_jf[0]['member_name'];

				//--//积分操作日志

				//--$back_points = -$red_points;

				//--$sql_bc = "insert into shopwwi_points_log (pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values ({$memberid,},{$membername},{$back_points},{$pl_addtime},'分红豆换现金','redpoints')";
                                                                                                                                
				//--Model() -> execute($sql_bc);
				
					 //分红豆操作日志

			      $back_points=-$red_points;

			      $sql_bc="insert into shopwwi_points_log(pl_memberid,pl_membername,pl_points,pl_addtime,pl_desc,pl_stage) values($memberid,'$membername',$back_points,$pl_addtime,'WAP端分红豆变现转入余额-分红豆减少','redpoints')";

			      Model()->execute($sql_bc);
								
			    //余额增加日志-分红豆兑换余额比例
				
			         $add_yue=$red_points/100;
					 
			       //Model()->execute('update shopwwi_member set available_predeposit = available_predeposit + '.$add_yue.' where member_id='.$_SESSION['member_id']);
			        //日志
			        $sql_bc = "insert into shopwwi_pd_log (lg_member_id,lg_member_name,lg_type,lg_av_amount,lg_add_time,lg_desc) values($memberid,'$membername','redtocash',". $add_yue .",$pl_addtime,'WAP端分红豆变现转入余额-余额增加')";
			        Model() -> execute($sql_bc);

			}

			output_data(array('update' => 1));
		} else {
			output_data(array('update' => 0));

		}

		//showDialog($message,'reload',$update ? 'succ' : 'error');

	}

	public function reds_pointslogOp() {

		$condition_arr = $list_log = array();

		$condition_arr['pl_memberid'] = $this -> member_info['member_id'];
		$condition_arr['pl_stage'] = 'redpoints';

		//分页

		$page = new Page();

		$points_model = Model('points');

		$list_log = $points_model -> getPointsLogList($condition_arr, $page, '*', '');
		if (!$list_log) {
			$list_log = array();
		}
		if (!empty($list_log)) {

			foreach ($list_log as $key => $value) {

				$list_log[$key]['stagetext'] = $this -> insertarr($value['pl_stage']);

				$list_log[$key]['addtimetext'] = date('Y-m-d H:i:s', $value['pl_addtime']);

			}

		}

		$page_count = $page -> gettotalpage();

		output_data(array('log_list' => $list_log), mobile_page($page_count));

	}

	public function kucun_pointslogOp() {

		$condition_arr = $list_log = array();

		$condition_arr['pl_memberid'] = $this -> member_info['member_id'];
		$condition_arr['pl_stage'] = 'kucunpoints';

		//分页

		$page = new Page();

		$points_model = Model('points');

		$list_log = $points_model -> getPointsLogList($condition_arr, $page, '*', '');
		if (!$list_log) {
			$list_log = array();
		}

		if (!empty($list_log)) {

			foreach ($list_log as $key => $value) {

				$list_log[$key]['stagetext'] = $this -> insertarr($value['pl_stage']);

				$list_log[$key]['addtimetext'] = date('Y-m-d H:i:s', $value['pl_addtime']);

			}

		}

		$page_count = $page -> gettotalpage();

		output_data(array('log_list' => $list_log), mobile_page($page_count));

	}

	//经验值
	public function exp_pointslogOp() {

		$condition_arr = $list_log = array();

		$condition_arr['exp_memberid'] = $this -> member_info['member_id'];

		//分页

		$page = new Page();

		$points_model = Model('exppoints');

		$list_log = $points_model -> getExppointsLogList($condition_arr, $page, '*', '');
		//exit(print_r(list_log));
		if (!$list_log) {
			$list_log = array();
		}

		if (!empty($list_log)) {

			foreach ($list_log as $key => $value) {

				$list_log[$key]['stagetext'] = $this -> insertarr($value['pl_stage']);

				$list_log[$key]['addtimetext'] = date('Y-m-d H:i:s', $value['pl_addtime']);

			}

		}

		$page_count = $page -> gettotalpage();

		output_data(array('log_list' => $list_log), mobile_page($page_count));

	}

	private function insertarr($stage) {

		switch ($stage) {

			case 'regist' :
				$insertarr = '注册会员';

				break;

			case 'login' :
				$insertarr = '会员登录';

				break;

			case 'comments' :
				$insertarr = '评论商品';

				break;

			case 'order' :
				$insertarr = '购物消费';

				break;

			case 'pointorder' :
				$insertarr = '兑换礼品';

				break;

			case 'signin' :
				$insertarr = '会员签到';

				break;
		}

		return $insertarr;

	}

	/**

	 * 检验是否能签到

	 */

	public function checksigninOp() {

		$condition = array();

		$condition['pl_memberid'] = $this -> member_info['member_id'];

		$condition['pl_stage'] = 'signin';

		$todate = date('Ymd000000');

		$totime = strtotime($todate);

		$condition['pl_addtime'][] = array('egt', $totime);

		$condition['pl_addtime'][] = array('elt', $totime + 86400);

		$points_model = Model('points');

		$log_array = $points_model -> getPointsInfo($condition);

		if (!empty($log_array)) {

			output_error('已签到');

		} else {

			$points_signin = intval(C('points_signin'));

			output_data(array('points_signin' => $points_signin));

		}

	}

	/**

	 * 签到 array('pl_memberid'=>'会员编号','pl_membername'=>'会员名称','pl_adminid'=>'管理员编号','pl_adminname'=>'管理员名称','pl_points'=>'积分','pl_desc'=>'描述','orderprice'=>'订单金额','order_sn'=>'订单编号','order_id'=>'订单序号','point_ordersn'=>'积分兑换订单编号');

	 */

	public function signin_addOp() {

		$points_signin = intval(C('points_signin'));
		//签到对得积分数

		$points_model = Model('points');

		$insertarr['pl_memberid'] = $this -> member_info['member_id'];

		$insertarr['pl_membername'] = $this -> member_info['member_name'];

		$insertarr['pl_points'] = $points_signin;

		$insertarr['pl_points'] = $points_signin;

		$return = $points_model -> savePointsLog('signin', $insertarr, false);

		output_data(array('point' => $return));

	}

}
